package com.unionftech.common.common.ui

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.coordinatorlayout.widget.CoordinatorLayout
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.unionftech.common.R
import com.unionftech.common.R.dimen
import com.unionftech.common.R.style
import com.unionftech.common.utils.StatusBarUtil
import com.unionftech.common.utils.Utils
import com.unionftech.common.common.bean.CountryInfo


class UFCountryCodeSelectDialogFragment private constructor(private val mContext: Context) :
    BottomSheetDialogFragment() {
    private var mCountryCodeFragment: UFCountryCodeSelectFragment? = null
    private var mCallBack: ((CountryInfo) -> Unit)? = null

    companion object {
        @JvmStatic
        fun newInstance(context: Context) = UFCountryCodeSelectDialogFragment(context).apply {
            arguments = Bundle().apply {}
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NORMAL, style.Theme_AndroidCRM_BottomSheetDialog)
        arguments?.let {}
        mCountryCodeFragment = UFCountryCodeSelectFragment.newInstance()
        mCountryCodeFragment!!.setCallBack {
            mCallBack?.invoke(it)
            dismiss()
        }
    }


    @SuppressLint("CommitTransaction")
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(
            R.layout.fragment_u_f_country_code_select_dialog, container, false
        )
        childFragmentManager.beginTransaction().replace(
            R.id.fl_dialog_content, mCountryCodeFragment!!
        ).commit()
        return view
    }

    fun setCallBack(callback: ((CountryInfo) -> Unit)?) {
        this.mCallBack = callback
    }

    @SuppressLint("NewApi")
    override fun onStart() {
        super.onStart()
        val dialog = dialog as BottomSheetDialog
        dialog.window?.apply {
            attributes.windowAnimations = style.BottomDialogAnimation
            //获取diglog的根部局
            val bottomSheet =
                dialog.delegate.findViewById<FrameLayout>(com.google.android.material.R.id.design_bottom_sheet)
            if (bottomSheet != null) {
                bottomSheet.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
                //获取根部局的LayoutParams对象
                val layoutParams: CoordinatorLayout.LayoutParams =
                    bottomSheet.layoutParams as CoordinatorLayout.LayoutParams
                layoutParams.height = getPeekHeight()
                //修改弹窗的最大高度，不允许上滑（默认可以上滑）
                bottomSheet.layoutParams = layoutParams

                val behavior: BottomSheetBehavior<FrameLayout> =
                    BottomSheetBehavior.from(bottomSheet)
                //peekHeight即弹窗的最大高度
                behavior.peekHeight = getPeekHeight()
                // 初始为展开状态
                behavior.state = BottomSheetBehavior.STATE_EXPANDED
            }
            Utils.setDialogNavigationBarTransparent(this)
        }
    }

    private fun getPeekHeight(): Int {
        val statusBarHeight = StatusBarUtil.getStatusBarHeight(mContext)
        val titleBarHeight = mContext.resources.getDimension(dimen.dp_44)
        val peekHeight =
            mContext.resources.displayMetrics.heightPixels - statusBarHeight - titleBarHeight
        //设置弹窗高度为屏幕高度-状态栏高度-导航栏的高度
        return peekHeight.toInt()
    }


    override fun onDestroy() {
        mCountryCodeFragment?.setCallBack(null)
        mCountryCodeFragment = null
        super.onDestroy()
    }
}